package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.api.BaseRest;
import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.AdminModel;
import com.xieyingwangluo.yxpart.service.AdminService;
import com.xieyingwangluo.yxpart.utils.StringBuildUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AdminLogic extends BaseRest {
    @Autowired
    private AdminService adminService;

    //注册
    public Result add(AdminModel adminModel) {
        adminModel.setNoid(StringBuildUtils.buildUUID());
        adminModel.setState(1);
        adminModel.setHead_img("/var/img/tomcat/yxpart/img-head.jpg");
        adminModel.setPower(1);

        if (adminModel.getTel() != null || !"".equals(adminModel.getTel())) {
            System.out.println(adminModel.getTel());
            List<AdminModel> list = adminService.selT(adminModel.getTel());
            if (list.size() > 0) {
                return Result.generate(1, "Admin already exists", null);
            }
        }
        if (adminModel != null) {
            adminService.add(adminModel);
            return Result.generate(0, "Add success", adminModel);
        }
        return Result.generate(1, "Add fail", null);
    }

    //更新用户
    public Result update(AdminModel adminModel) {
        AdminModel admin = adminService.selO(adminModel.getNoid());
        if (adminModel.getTel() != null) {
            List<AdminModel> list = adminService.selT(adminModel.getTel());
            if (list.size() == 0) {
                adminModel.setNoid(admin.getNoid());
                int cnt = adminService.upd(adminModel);
                return Result.generate(0, "Upd success", cnt);
            }
        }
        int cnt = adminService.upd(adminModel);
        if (cnt > 0) {
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    //登录
    public Result checklogin(HttpSession session,
                             String name,
                             String pwd) {
        Map<String, Object> map = new HashMap<>();
        AdminModel adminModel = adminService.checklogin(name, pwd);
        if (adminModel != null) {
            session.setAttribute("admin", adminModel);
            map.put("Login", "管理员");
            map.put("User", adminModel);
            return Result.generate(0, "Login success", map);
        }
        return Result.generate(1, "Login fail", null);
    }


    //登录
    public Result login(HttpSession session,
                        String tel,
                        String pwd) {
        Map<String, Object> map = new HashMap<>();
        AdminModel adminModel = adminService.login(tel, pwd);
        if (adminModel != null) {
            session.setAttribute("admin", adminModel);
            map.put("Login", "管理员");
            map.put("User", adminModel);
            return Result.generate(0, "Login success", map);
        }
        return Result.generate(1, "Login fail", null);
    }


    //更新管理员密码
    public Result updPwd(String noid, String pwd, String oldpwd) {
        AdminModel admin = adminService.selO(noid);

        AdminModel adminModel = adminService.selO(admin.getNoid());
        if (oldpwd.equals(adminModel.getPwd())) {
            int cnt = adminService.updPwd(admin.getNoid(), pwd);
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    //模糊查询管理员用户
    public Result selA(AdminModel adminModel) {
        List<AdminModel> list = adminService.selA(adminModel);
        if (list.size() > 0) {
            return Result.generate(0, "Select success", list);

        }
        return Result.generate(1, "Select fail", list);
    }

    //删除用户
    public Result del(String noid) {
        if (noid != null || !noid.equals("")) {
            int cnt = adminService.del(noid);
            return Result.generate(0, "Del success", cnt);
        }
        return Result.generate(1, "Del fail", null);
    }

    //根据id获取用户信息
    public Result selO(String noid) {
        AdminModel adminModel = adminService.selO(noid);
        adminModel.setPwd("");
        if (adminModel != null) {
            return Result.generate(0, "Select success", adminModel);
        }
        return Result.generate(1, "Select fail", null);
    }
}
